package com.example.handlerdemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.ImageView;
import android.widget.ViewFlipper;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
private Handler mHandler;
private int imageIndex=0;
/*private ViewFlipper flipper;
private Message message;*/
private int[] imgs=new int[]{R.mipmap.img_miao1,R.mipmap.img_miao2,R.mipmap.img_miao3,R.mipmap.img_miao4,
        R.mipmap.img_miao5,R.mipmap.img_miao6,R.mipmap.img_miao7,R.mipmap.img_miao8,
        R.mipmap.img_miao9,R.mipmap.img_miao10,R.mipmap.img_miao11,R.mipmap.img_miao12,
        R.mipmap.img_miao13,R.mipmap.img_miao14,R.mipmap.img_miao15,R.mipmap.img_miao16,
        R.mipmap.img_miao17,R.mipmap.img_miao18,R.mipmap.img_miao19,R.mipmap.img_miao20,
        R.mipmap.img_miao21,R.mipmap.img_miao22,R.mipmap.img_miao23,R.mipmap.img_miao24,
        R.mipmap.img_miao25,R.mipmap.img_miao26};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }
    private void initView() {
        ImageView imageView = findViewById(R.id.image_anim);
       // mHandler=new Handler(getMainLooper());
       /* {
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                switch (msg.what){
                    case 0xf1:
                        imageView.setImageResource(imgs[imageIndex++%26]);
                        break;
                }
            }
        };*/
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {

                //早期线程切换
               /* mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        //主线程执行区域
                        imageView.setImageResource(imgs[imageIndex++%26]);
                    }
                });*/
                //因为在一个应用内只有一个主线程  故可以不进行Handler对象的创建  常用的子线程切换主线程
               runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        imageView.setImageResource(imgs[imageIndex++%26]);
                    }
                });

            }
        }, 0, 300);



       /* for (int i = 0; i < imgs.length; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(imgs[i]);
            flipper.addView(imageView);

        }
        message = Message.obtain();
        message.what = 0xf1;
        mHandler.sendMessage(message);
       */

    }


   /* Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0xf1:
                    flipper.showPrevious();
                    message = mHandler.obtainMessage(0xf1);
                    mHandler.sendMessageDelayed(message, 1000);
                    break;
            }
        }
    };*/

}